import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;

/**
 * @author lys
 * 添加类别页面
 */
public class AddType extends JFrame {
    private static AddType instance;

    private JLabel jLabel = new JLabel("增加类别");
    private JLabel jLabel1 = new JLabel("新增类别 : ");

    private JTextArea jTextArea = new JTextArea();

    private JButton jButton1 = new JButton("确认提交");
    private JButton jButton2 = new JButton("返回");

    private JPanel jPanel = new JPanel();

    private Font font1 = new Font("宋体",Font.BOLD,60);
    private Font font2 = new Font("宋体",Font.BOLD,40);
    private Font font3 = new Font("宋体",Font.BOLD,30);

    private AddType() {
        Icon i = new ImageIcon(AddType.class.getResource("img/login2.jpg"));
        JLabel Label = new JLabel(i);
        Label.setBounds(0, 0, 800, 800);

        jLabel.setFont(font1);
        jLabel.setBounds(280, 50, 300, 80);
        jLabel.setForeground(Color.white);

        jLabel1.setFont(font2);
        jLabel1.setBounds(150, 270, 200, 50);
        jLabel1.setForeground(Color.white);
        //jTextArea1.setLayout(null);
        jTextArea.setPreferredSize(new Dimension(350,50));
        jTextArea.setBorder(BorderFactory.createLineBorder(Color.white,3));
        jTextArea.setFont(font2);
        jTextArea.setForeground(Color.black);
        jPanel.setBounds(0,260,800,60);
        jPanel.add(jLabel1);
        jPanel.add(jTextArea);

        jButton1.setBounds(280,550,300,60);
        jButton1.setFont(font2);
//        jButton1.setForeground(Color.white);
        jButton2.setBounds(620,650,100,50);
//        jButton2.setForeground(Color.white);
        jButton2.setFont(font3);

        jPanel.setOpaque(false);
        jTextArea.setOpaque(false);

        add(jLabel);
        add(jPanel);
        add(jButton1);
        add(jButton2);
        add(Label);

        setVisible(true);
        setSize(800,800);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String type = jTextArea.getText();
                addType(type);
                ManageBook.getInstance().showBooks();
                removeInstance();
                AddBook.removeInstance();
                DeleteBook.removeInstance();
                UpdateBook.removeInstance();
                DeleteType.removeInstance();
                UpdateType.removeInstance();
            }
        });

        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                AdminInterface.getInstance().admin.setVisible(true);
            }
        });

        jButton1.setFocusPainted(false);
        jButton2.setFocusPainted(false);
    }

    public static AddType getInstance() {
        if(instance==null) instance=new AddType();
        return instance;
    }

//    public static void main(String[] args) {
//        AddType.getInstance();
//    }
    /**
     * @author zj
     */
    /**
     * 添加类别到数据库
     * @param type
     */
    public void addType(String type){
        if(type.length() == 0){
            JOptionPane.showMessageDialog(null, "输入信息不能为空！");
            return;
        }
        String sql = "insert into typelist values(?)";
        try{
            PreparedStatement preSql = DatabaseConnection.getInstance().getCon().prepareStatement(sql);
            preSql.setString(1,type);
            int ok = preSql.executeUpdate();
            System.out.println(ok+ " line is affected");
            if(ok > 0){
                JOptionPane.showMessageDialog(null, "增加类别成功");
            }
        }catch (Exception e){
            System.out.println(e);
        }
        setVisible(false);
        AdminInterface.getInstance().admin.setVisible(true);
    }
    public static void removeInstance(){
        instance = null;
    }
}
